/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package somsinfinal;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;

/**
 *
 * @author Somsin Corp.
 */
public class Board extends JPanel implements ActionListener, MouseMotionListener, KeyListener, MouseListener {

    Tank t;
    turret tu;
    Image img;
    Timer time;
    int x, y;
    static Font font = new Font("SansSerif", Font.BOLD, 28);  //Sets Ammo and Fuel Font in Bottom Bar
    static Font font2 = new Font("SansSerif", Font.BOLD, 18);

    public Board() {
        t = new Tank();
        tu = new turret();
        ImageIcon i = new ImageIcon("./images/imagesMore/tank-interface.jpg");

        setFocusable(true);

        img = i.getImage();
        time = new Timer(20, this);
        time.start();

        //       addMouseMotionListener(new e);   
        addKeyListener(new AL());
    }

    @Override
    public void actionPerformed(ActionEvent e) {
//        throw new UnsupportedOperationException("Not supported yet.");
       if (t.getFuel() > 0) {
            t.move();
        } else  {
           
   System.out.println("You are out of Fuel !!!!!!");
   
        }

        ArrayList bullets = Tank.getBullets();
        int b = 0;
        for (int w = 0; w < bullets.size(); w++) {
          
            Bullet m = (Bullet) bullets.get(w);

            if (m.getVisible() == true) {
                m.move();
            } else {
                bullets.remove(w);
            }
        }
        

    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.drawImage(img, 0, 0, null);

        g2d.setFont(font2);
        g2d.setColor(Color.WHITE);
        g2d.drawString("'Copyright © Somsin 2012'", 10, 555);
        g2d.setFont(font);
        g2d.drawString("Ammo     " + t.getAmmo(), 274, 548);
        g2d.drawString("Fuel    " + t.getFuel(), 570, 548);

        g2d.drawImage(t.getImage(), t.getX(), t.getY() + 20, null);

        double Yd = (y - tu.getTurretY());
        double Xd = (x - tu.getTurretX());

        double radAngle = Math.atan2(Yd, Xd);

        g2d.rotate(radAngle, t.getX() + 21, t.getY() + 48);
        g2d.drawImage(tu.getImage(), t.getX() + 5, t.getY() + 33, null);

        ArrayList bullets = Tank.getBullets();
        for (int w = 0; w < bullets.size(); w++) {
            Bullet m = (Bullet) bullets.get(w);
            g2d.drawImage(m.getImage(), m.getX() + 55, m.getY() + 40, null);
        }
        repaint();
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        //  throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void mouseMoved(MouseEvent me) {
        x = me.getX();
        y = me.getY();
        System.out.println(me.getX() + " " + me.getY());
        //        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void keyTyped(KeyEvent e) {
    //    throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void keyPressed(KeyEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
        t.keyPressed(e);
    }

    @Override
    public void keyReleased(KeyEvent e) {
        t.keyReleased(e);
        // throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if (t.getAmmo() != 0) {
       //    t.setAmmo(t.getAmmo() - 1);
            new Audio("./images/fire.wav").start();
            t.fire();
        }
        //  else{
    //        throw new OutOfFuelException("Not supported yet.");
        // }
    }

    @Override
    public void mousePressed(MouseEvent e) {
        //     throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        //     throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        //       throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void mouseExited(MouseEvent e) {
        //       throw new UnsupportedOperationException("Not supported yet.");
//    }
    }

    public class AL extends KeyAdapter {

        @Override
        public void keyReleased(KeyEvent e) {
            t.keyReleased(e);
            //          tu.keyReleased(e);
        }

        @Override
        public void keyPressed(KeyEvent e) {
            t.keyPressed(e);
//              tu.keyPressed(e);
        }
    }
}